﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sar
{
    public partial class FormDML : Form
    {
        #region ctor
        public FormDML(FormMain formMain, String operation)
        {
            InitializeComponent();
            this.formMain = formMain;
            this.operation = operation;
            this.buttonOK.Text = operation;
            switch (operation)
            {
                case "Update":
                    this.textBoxCommandNameModify.Visible = true;
                    break;
                case "Delete":
                    this.label2.Enabled = false;
                    this.textBoxCommandLine.Enabled = false;
                    break;
                default:
                    break;
            }
        }
        #endregion


        #region field
        private FormMain formMain = null;
        private String operation = null;
        #endregion


        #region methods
        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                switch (this.operation)
                {
                    case "Insert":
                        DataRow rowNew = this.formMain.Table.NewRow();
                        String prefixNO = DateTime.Now.ToString("yyyyMMdd");
                        rowNew["CommandID"] = Guid.NewGuid();
                        rowNew["CommandNO"] = (Convert.ToInt64(prefixNO) * 10000 + this.formMain.Table.Select("CommandNO LIKE '" + prefixNO + "%'").Length).ToString();
                        rowNew["CommandName"] = this.textBoxCommandName.Text;
                        rowNew["CommandLine"] = this.textBoxCommandLine.Text;
                        this.formMain.Table.Rows.Add(rowNew);
                        break;
                    case "Update":
                        DataRow[] rowUpdate = this.formMain.Table.Select("CommandName = '" + this.textBoxCommandName.Text + "'");
                        rowUpdate[0]["CommandName"] = this.textBoxCommandName.Text;
                        rowUpdate[0]["CommandLine"] = this.textBoxCommandLine.Text;
                        break;
                    case "Delete":
                        DataRow[] rowDelete = this.formMain.Table.Select("CommandName = '" + this.textBoxCommandName.Text + "'");
                        rowDelete[0].Delete();
                        break;
                    default:
                        break;
                }
                this.formMain.Opr.Adapter.Update(formMain.Table);
                this.Dispose();

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message + "\n"
                                + ex.Source + "\n"
                                + ex.StackTrace + "\n"
                                + ex.TargetSite.ToString() + "\n");
            }
        }
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        private void textBoxCommandName_Leave(object sender, EventArgs e)
        {
            if (this.operation == "Update")
            {
                DataRow[] rowUpdate = this.formMain.Table.Select("CommandName = '" + this.textBoxCommandName.Text + "'");
                if (rowUpdate.Length == 1)
                {
                    this.textBoxCommandNameModify.Text = rowUpdate[0]["CommandName"].ToString();
                    this.textBoxCommandLine.Text = rowUpdate[0]["CommandLine"].ToString();
                }
            }
        }
        #endregion
    }
}
